package com.kfzx.www.emptyviewdemo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Author by Li_Rihong,
 * Email 598116345@qq.com
 * Date on 2018/8/7 19:57.
 * description:继承自recyclerview，重写了adapterDataObserver，实时的监听数据是不是为空，为空就显示空白图片
 */
public class EmptyRecyclerView extends RecyclerView {
    //显示的空白占位图
    private View mEmptyView;
    //重写adapter的数据观察者
    private AdapterDataObserver mObserver = new AdapterDataObserver() {
        @Override
        public void onChanged() {
//            super.onChanged();
            //数据以改变就需要重新的监听，判断是不是为空等
            checkIfEmpty();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
//            super.onItemRangeChanged(positionStart, itemCount);
            checkIfEmpty();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {
//            super.onItemRangeChanged(positionStart, itemCount, payload);
            checkIfEmpty();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
//            super.onItemRangeInserted(positionStart, itemCount);
            checkIfEmpty();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
//            super.onItemRangeRemoved(positionStart, itemCount);
            checkIfEmpty();
        }

        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
//            super.onItemRangeMoved(fromPosition, toPosition, itemCount);
            checkIfEmpty();
        }
    };

    public EmptyRecyclerView(@NonNull Context context) {
        super(context);
    }

    public EmptyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public EmptyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /***
     * 判断数据是不是为空的，如果时空的
     */
    private void checkIfEmpty() {
        //判断是不是为空，不为空的时候 才能判断，
        if (mEmptyView != null && getAdapter() != null) {
            //数据为空
            boolean emptyViewVisible = getAdapter().getItemCount() == 0;
            //显示空白图
            mEmptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            //隐藏当前的recyclerview
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }

    @Override
    public void setAdapter(@Nullable Adapter adapter) {
        Adapter oldAdapter = getAdapter();
        if (oldAdapter != null) {
            //先解绑旧的观察者
            oldAdapter.unregisterAdapterDataObserver(mObserver);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            //绑定新的观察者到新的adapter
            adapter.registerAdapterDataObserver(mObserver);
        }
        checkIfEmpty();
    }

    /***
     * 设置空白的视图
     * @param emptyView
     */
    public void setEmptyView(View emptyView) {
        this.mEmptyView = emptyView;
        // TODO: 2018/8/7 记得添加这句话，显示不出空的图片
        ((ViewGroup) this.getRootView()).addView(mEmptyView);
        checkIfEmpty();
    }
}
